// pages/songPackage/pages/playlist/playlist.js
import request from '../../../../utils/request'
let firstseanum = '';
let isSend = false;
let firstshowToast = true;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        musicList:[],
        compshow:false,
        toggleDelay:false,
        search:[],            //搜索列表
        searchLength:0,
        before:0

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 歌单列表获取数据
        this.gettopsonger()
    },

    async gettopsonger(){
        let { before ,musicList} = this.data
        let res = await request('/top/playlist/highquality', { limit:40,before});
        musicList.push(...res.playlists)
        // console.log(musicList)
        this.setData({
          before:res.lasttime,
          musicList
          
        })
        // console.log(res)
    },
    gotolists(event){
      if(this.data.compshow){
        this.setData({
          compshow:false
         })
         return
      }
        let {listid} = this.data
       //  console.log(event.currentTarget.dataset.song.id)
       listid = event.currentTarget.dataset.song.id
       wx.navigateTo({
         url: '/pages/songPackage/pages/playListDetail/playListDetail?listid='+listid
       })
     },
     showcomp(){
       this.setData({
        compshow:true
       })
     },
     hiddencomp(){
      this.setData({
        compshow:false
       })
     },
     cliTag(event){
       let tag = event.detail.tag
       this.getNewlist(tag)
     },

     //获取新歌单
     async getNewlist(tag){
       let list = []
       let res1 = await request("/top/playlist",{cat:tag})
       let res2 = await request("/top/playlist/highquality" ,{cat:tag})
       list = res2.playlists.concat(res1.playlists)
      //  console.log(list)
      this.setData({
        musicList:list
       })
       
     },

     handleInputChange(event){
        this.setData({
          searchContent: event.detail.value.trim(),
        })
          if(isSend){
            return
          }
          isSend = true;
          
          // 函数节流
        setTimeout( () => {
          isSend = false;
        }, 200)
  
    },
    chudi(){
      //   console.log("854594")
        if(this.data.search.length!==0){
          this.searchplayList()
        }else{
          this.gettopsonger()
        }
       
      },
    

    //搜索歌单
    async searchplayList(){
      let {searchContent} = this.data
      if(!searchContent){
        // console.log("空")
        wx.showToast({
          title: '输入不能为空！',
          icon:'error',
          duration:1000,
          mask:true,
          success:function(){
            firstshowToast = false
          }
        })
      }else{
        if(!firstseanum){
          this.setData({
             search:[],
             searchLength:0
           })
           firstseanum=searchContent
        }else if(firstseanum !==searchContent){
          this.setData({
            search:[],
            searchLength:0
          })
          firstseanum=searchContent
          firstshowToast=true
        }
        // console.log(this.data.search)
        let {search , searchLength} = this.data
        let res = await request('/cloudsearch',{type:1000,keywords:searchContent,limit:30,offset:searchLength})
        if(!res.result.playlists){
          if(firstshowToast){
            wx.showToast({
              title: '没有更多了',
              icon:'error',
              duration:1000,
              mask:true,
              success:function(){
                firstshowToast = false
              }
            })
          }
        }
        console.log(res)
        // res.result.playlists.forEach((item,index)=>{
        //   if(item.playCount>10000){
        //      item.playCount = Math.trunc(item.playCount/10000) + "万"        
        //   }
        // })
    
        search.push(...res.result.playlists)
     //    console.log(search)
     //    console.log(searchLength)
        searchLength = search.length
        this.setData({
         search,
         searchLength
        })
      }

     },




     swiperchange(event){
       // let {TabCur} = this.data
       this.setData({
         _TabCur:event.detail.current
       })
     },



    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})